package com.ruiysoft.service.order.consts;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Date : 2018/11/3
 *
 * @author WenJialu
 */
public enum OrderDBStatusEnum {
	/*
	* 1 订单生成 待用户确认
            2 待用户付款
            3 用户已付款 待平台确认
            4 平台已确认 待商铺确认
            5 商铺确认付款 待发货
            6 已发货 待确认收货
            7 已收货
            11 申请换货 待商铺确认
            12 商铺确认换货
            21 申请退货 待商铺确认
            22 商铺确认退货 待退款
            23 已退款
            97 订单取消
            98 订单关闭
            99 订单完结*/

	ORDER_CREATE_WAITING_FOR_CUSTOMER_CONFIRM(1, "订单生成，待用户确认"),
	CUSTOMER_CONFIRMED_WAITING_FOR_PAYMENT(2, "待用户付款"),
	CUSTOMER_PAID_WAITING_FOR_PLATFORM_CONFIRM(3, "用户已付款，待平台确认"),
	PLATFORM_CONFIRMED_WAITING_FOR_SHOP_CONFIRM(4, "平台已确认 待商铺确认"),
	SHOP_CONFIRMED_WATTING_FOR_DELIVER(5, "商铺确认付款 待发货"),
	DELIVERED(6, "已发货"),
	RECEIVED(71, "已收货"),
	SYSTEM_SIGNED(72, "系统签收"),
	BARTER_WAITING_FOR_SHOP_CONFIRM(11, "申请换货 待商铺确认"),
	BARTER_SHOP_CONFIRMED(12, "商铺确认换货"),
	RETURNED_PURCHASE_WATING_FOR_SHOP_CONFIRM(21, "申请退货 待商铺确认"),
	RETURNED_PURCHASE_CONFIRMED_WATING_FOR_REFUND(22, "商铺确认退货 待退款"),
	REFUNDED(23, "已退款"),
	ORDER_CANCELLED(97, "订单取消"),
	ORDER_CLOSED(98, "订单关闭"),
	ORDER_OVER(99, "订单完结");


	private Integer code;
	private String text;

	private static Set<Integer> allDBStatus = null;
	private static Set<Integer> dbStatusCancelable = null;
	private static Set<Integer> dbStatusReturnable = null;
//	private static Set<Integer> dbStatusConfirmReceiptable = null;
	private static Map<Integer, OrderDBStatusEnum> typeEnumMap = null;

	OrderDBStatusEnum(Integer code, String text) {
		this.code = code;
		this.text = text;
	}

	public static Set<Integer> getAllDBStatus() {
		if (null == allDBStatus) {
			allDBStatus = Arrays.stream(OrderDBStatusEnum.values()).map(OrderDBStatusEnum::getCode).collect(Collectors.toSet());
		}
		return allDBStatus;
	}

	public static Set<Integer> getDBStatusCancelable(){
		if (null == dbStatusCancelable) {
			dbStatusCancelable = new HashSet<>(Arrays.asList(ORDER_CREATE_WAITING_FOR_CUSTOMER_CONFIRM.getCode()
					,CUSTOMER_CONFIRMED_WAITING_FOR_PAYMENT.getCode()));
		}
		return dbStatusCancelable;
	}

	/*public static Set<Integer> getDBStatusConfirmReceiptable(){
		if (null == dbStatusConfirmReceiptable) {
			dbStatusConfirmReceiptable = new HashSet<>(Arrays.asList(ORDER_CREATE_WAITING_FOR_CUSTOMER_CONFIRM.getCode()
					,CUSTOMER_CONFIRMED_WAITING_FOR_PAYMENT.getCode()));
		}
		return dbStatusConfirmReceiptable;
	}*/

	public static Set<Integer> getDBStatusReturnable(){
		if (null == dbStatusReturnable) {
			dbStatusReturnable = new HashSet<>(Arrays.asList(CUSTOMER_PAID_WAITING_FOR_PLATFORM_CONFIRM.getCode()
					,PLATFORM_CONFIRMED_WAITING_FOR_SHOP_CONFIRM.getCode()));
		}
		return dbStatusReturnable;
	}

	public static OrderDBStatusEnum getInstanceByCode(Integer code){
		if (null == typeEnumMap) {
			typeEnumMap = Arrays.stream(OrderDBStatusEnum.values()).collect(Collectors.toMap(OrderDBStatusEnum::getCode, typeEnum -> typeEnum));
		}
		return typeEnumMap.get(code);
	}



	public static boolean existsStatus(Set<Integer> status) {
		return getAllDBStatus().containsAll(status);
	}

	public Integer getCode() {
		return code;
	}

	public String getText() {
		return text;
	}
}
